package com.synjones.ui.others;

import android.util.Pair;

import com.synjones.AppExecutors;
import com.synjones.Constant;
import com.synjones.entry.SignPerson;
import com.synjones.network.Api;
import com.synjones.printer.billPrinter.BillPrinterAble;

import java.util.List;

public class OthersSecPresenter implements OthersContract.Presenter {

    private OthersContract.View view;
    private BillPrinterAble printerAble;

    public OthersSecPresenter(OthersContract.View view, BillPrinterAble printerAble) {
        this.view = view;
        this.printerAble = printerAble;
    }

    @Override
    public void search(final String name) {
        AppExecutors.ioExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final Pair<String, List<SignPerson>> pair= Api.dataByName(name);
                AppExecutors.mainThread.execute(new Runnable() {
                    @Override
                    public void run() {
                        switch (pair.first){
                            case "200":
                                view.showSearchResult(pair.second);
                                break;
                            case "500":
                                view.showBackInfo("服务器错误", Constant.Color.COLOR_FAIL);
                                break;
                            case "600":
                                view.showBackInfo("网络错误", Constant.Color.COLOR_FAIL);
                                break;
                        }
                    }
                });
            }
        });

    }

    @Override
    public void sign(final String oneSelf, final String presenterName,
                     final String presenterIdno, final String invitationCode,
                     final String idno, final SignPerson signPerson) {
        AppExecutors.ioExecutor.execute(new Runnable() {
            @Override
            public void run() {
                final String code = Api.sign(
                        invitationCode,
                        presenterName,
                        oneSelf,
                        "",
                        presenterIdno,
                        idno);
                AppExecutors.mainThread.execute(new Runnable() {
                    @Override
                    public void run() {
                        switch (code){
                            case "500":
                                view.showBackInfo("服务器错误", Constant.Color.COLOR_FAIL);
                                break;
                            case "600":
                                view.showBackInfo("网络错误", Constant.Color.COLOR_FAIL);
                                break;
                            default:
                                if (code.equals("signed"))  {
                                    view.showBackInfo("重复签到", Constant.Color.COLOR_SUCCESS);
                                }
                                else if (code.equals("pushFail"))
                                    view.showBackInfo("同步失败", Constant.Color.COLOR_SUCCESS);
                                else {
                                    view.showBackInfo("签到成功", Constant.Color.COLOR_SUCCESS);
                                    AppExecutors.ioExecutor.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (oneSelf.equals("1")){
                                                signPerson.setDocumentNumber(idno.substring(0,4)
                                                        + "**********"
                                                        +idno.substring(idno.length()-4));
                                                printerAble.printBill(signPerson);
                                            }else {
                                                printerAble.printBill(signPerson);
                                            }
                                        }
                                    });
                                }
                                break;
                        }

                    }
                });
            }
        });
    }
}
